<?php

namespace app\admin\controller;

use think\Db;

class Web extends Authorized
{

    public function index(){
        $get_data=$this->request->get();
        $userid = $get_data['userid'];
        $this->assign('userid', $userid);
        return $this->fetch();
    }
    public function qf(){return $this->fetch();}
    public function stop(){return $this->fetch();}
    public function info(){
        $article=Db::table('tb_webinfo')->where('id' , input('id'))->find();
        $this->assign('article', $article);
        return $this->fetch();
    }


    public function getList()
    {
        $get_data=$this->request->get();
        $post_data = $this->request->post();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $keywords = input('keywords');
        $where=[];

        if (!empty($get_data['userid'])){
            $userid = $get_data['userid'];
            $where['w.userID']=$userid;
        }

        if (!empty($keywords)){
            $where['w.urlName']=['like','%'.$keywords.'%'];
        }


        $count=Db::table('tb_webinfo w')->join('tb_admin a','a.id=w.seoid','left')->where($where)->count();
        $rows=Db::table('tb_webinfo w')->field('w.*,a.name seo_name')
            ->join('tb_admin a','a.id=w.seoid','left')
            ->where($where)
            ->order('state,id desc')->page($page, $limit)->select();
        return json(['code' =>0,'count' =>$count,'msg' =>'','data'=>$rows]);
    }

    public function add()
    {
        $get_data = $this->request->get();

        if (!empty($get_data['userID'])){
            $userid = $get_data['userID'];
            $this->assign('userID', $userid);
        }

        $post_data = $this->request->post();

        if($post_data){
//            return json(['code' =>1,'count' =>0,'msg' =>'添加失败','data'=>$post_data]);
            Db::table('tb_webinfo')->startTrans();
            $id =Db::table('tb_webinfo')->insertGetId($post_data);
            if (false === $id) {
                Db::table('tb_webinfo')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'添加失败','data'=>'']);
            } else {
               Db::table('tb_webinfo')->commit();
               Db::table('tb_user')->where('id',$post_data['userID'])->setInc('webNum'); //用户网站数量增加
               return json(['code' =>0,'count' =>0,'msg' =>'添加成功','data'=>'']);
            }
        }

       return $this->fetch();
    }

    public function edit()
    {
        $post_data = $this->request->post();
        if($post_data){
            $id=array_shift($post_data);
            $result =Db::table('tb_webinfo')->where('id',$id)->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }

        $article=Db::table('tb_webinfo')->where('id' , input('id'))->find();
        $this->assign('article', $article);
       return $this->fetch();
    }
//分配优化师
    public function set_seo()
    {
        $post_data = $this->request->post();
        if($post_data){
            $id=$post_data['id'];
            $seoid=$post_data['seoid'];
            $result =Db::table('tb_webinfo')->where('id',$id)->setField(['seoid'=>$seoid,'ApplyNum'=>0,'gotime'=>date('Y-m-d H:i:s',time())]);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                Db::table('tb_webkey')->where('webid',$id)->setField('seoid',$seoid);
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }
    }
//    释放优化师
    public function reset_seo()
    {
        $post_data = $this->request->post();
        if($post_data){

            $result =Db::table('tb_webinfo')->where('id', $post_data['id'])->setField(['seoid'=>0,'gotime'=>null]);
            if (false === $result) {
                return json(['msg' =>'释放失败', 'code' =>1]);
            } else {

                Db::table('tb_webkey')->where('webID', $post_data['id'])->setField(['seoid'=>0]);
                Db::table('tb_webinfoApply')->where('webid', $post_data['id'])->delete();
                return json(['msg' =>'释放成功', 'code' =>0]);
            }
        }
    }

//更新网站状态 1 3
    public function state_web()
    {
        $post_data = $this->request->post();
        if($post_data){
            Db::table('tb_webinfo')->startTrans();
            //更新状态

            if($post_data['state']==3){
                $keyNum=Db::table('tb_webinfo')->field('keyNum')->where('id', $post_data['id'])->find();

                $result =Db::table('tb_webinfo')->where('id', $post_data['id'])->setField([
                    'state'=>3,'keyNum'=>0,'stoptime'=>date('Y-m-d H:i:s',time())]);
                if (false === $result) {
                    Db::table('tb_webinfo')->rollback();
                    return json(['code' =>1,'count' =>0,'msg' =>'停用失败','data'=>'']);
                } else {
                    Db::table('tb_webinfo')->commit();

                    Db::table('tb_user')->where('id',$post_data['userID'])->update([
                        'keyNum'=>Db::raw('keyNum-'.$keyNum['keyNum']),
                        'webNum'=>Db::raw('webNum-1'),
                    ]);  //更新用户网站数量
                    Db::table('tb_webkey')->where('webID',$post_data['id'])->setField('state',3); //更新站点下关键词数量
                    return json(['code' =>0,'count' =>0,'msg' =>'停用成功','data'=>'']);
                }
            }

            if($post_data['state']==1){
                $result =Db::table('tb_webinfo')->where('id', $post_data['id'])->setField(['state'=>1,'stoptime'=>null]);
                if (false === $result) {
                    Db::table('tb_webinfo')->rollback();
                    return json(['code' =>1,'count' =>0,'msg' =>'停用失败','data'=>'']);
                } else {
                    Db::table('tb_webinfo')->commit();

                    Db::table('tb_user')->where('id',$post_data['userID'])->setInc('webNum');  //更新用户网站数量
//                    Db::table('tb_webkey')->where('webID',$post_data['id'])->setField('state',3); //更新站点下关键词数量
                    return json(['code' =>0,'count' =>0,'msg' =>'停用成功','data'=>'']);
                }
            }

        }
    }




    public function del()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('tb_webinfo')->where('id', $post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' =>'删除失败', 'code' =>1]);
            } else {
                return json(['msg' =>'删除成功', 'code' =>0]);
            }
        }
    }
}